package com.yeshine.opendata.service.impl;

import com.yeshine.opendata.core.ServiceBase;
import com.yeshine.opendata.dao.IDataThemeDao;
import com.yeshine.opendata.entity.DataTheme;
import com.yeshine.opendata.service.IDataThemeService;
import com.yeshine.opendata.utils.FileUtils;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import com.yeshine.opendata.utils.UploadConstantPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * Created by taohj on 2014/8/13.
 */
@Service
public class DataThemeServiceImpl extends ServiceBase<DataTheme> implements IDataThemeService {

    @Autowired
    private IDataThemeDao dao;

    private String path = UploadConstantPath.APPTHEME_FILE_PATH;


    @Override
    public Result<DataTheme> create(DataTheme dataTheme) {
        return new Result<DataTheme>(dao.insert(dataTheme) > 0);
    }

    @Override
    public Result<DataTheme> remove(DataTheme dataTheme) {
        return new Result<DataTheme>(dao.delete(dataTheme) > 0);
    }

    @Override
    public Result<DataTheme> modify(DataTheme dataTheme) {
        return new Result<DataTheme>(dao.update(dataTheme) > 0);
    }

    @Override
    public Result<DataTheme> getById(Integer id) {
        return new Result<DataTheme>(dao.selectById(id));
    }

    @Override
    public Result<DataTheme> getList(DataTheme dataTheme) {
        return new Result<DataTheme>(dao.selectList(dataTheme));
    }

    @Override
    public Result<DataTheme> getByPage(PageResult<DataTheme> pageResult) {
        pageResult.setRows(dao.selectbyPage(pageResult));
        return new Result<DataTheme>(pageResult);
    }

    @Override
    public Result<DataTheme> create(DataTheme theme, HttpServletRequest request) {

        Result<DataTheme> result = this.create(theme,request);
        if (!result.isSuccess()) {
            return result;
        }
        MultipartFile image = theme.getImageFile();
        if (image != null && image.getSize() > 0) {
            File target = new File(request.getSession().getServletContext().getRealPath(this.path), theme.getId() + "_" + image.getOriginalFilename());
            try {
                FileUtils.copy(image.getInputStream(), target);
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<DataTheme>(false, e.getMessage());
            }
            theme.setImage(this.path + theme.getId() + "_" + image.getOriginalFilename());
        }
        result = this.modify(theme);
        if (result.isSuccess()) {
            return new Result<DataTheme>("新增成功");
        } else {
            return new Result<DataTheme>("新增失败");
        }

    }


    @Override
    public Result<DataTheme> modify(DataTheme theme, HttpServletRequest request) {

        Result<DataTheme> result = null;
        MultipartFile image = theme.getImageFile();
        if (image != null && image.getSize() > 0) {
            File target = new File(request.getSession().getServletContext().getRealPath(this.path), theme.getId() + "_" + image.getOriginalFilename());
            try {
                FileUtils.copy(image.getInputStream(), target);
            } catch (IOException e) {
                e.printStackTrace();
                return new Result<DataTheme>(false, e.getMessage());
            }
            theme.setImage(this.path + theme.getId() + "_" + image.getOriginalFilename());
        }
        result = this.modify(theme);
        if (result.isSuccess()) {
            return new Result<DataTheme>("更新成功");
        } else {
            return new Result<DataTheme>("更新失败");
        }
    }
}
